$import("com.joypard.platform.security.role.RoleView");

/**
 * 角色管理-控制器
 */
K.defCtrl("com.joypard.platform.security.role.RoleCtrl", {
	roleView : null,
	// 初始化
	RoleCtrl : function() {
		this.roleView = RoleView.instance(this);

		E.addEvents("addRoleSuccessEvent");
		E.addEvents("updateRoleSuccessEvent");
		E.addEvents("delRolesSuccessEvent");
		E.addEvents("getUsersByRoleIdSuccessEvent");

		E.addListener("delRolesEvent", this.onDelRoles, this);
		E.addListener("addRoleEvent", this.onAddRole, this);
		E.addListener("updateRoleEvent", this.onUpdateRole, this);
	},

	/**
	 * 新增角色
	 */
	onAddRole : function(formElement) {
		var input = K.input({
			form : formElement,
			success : function(text) {
				var output = K.createOutput(text);
				if (!output.showError()) {
					E.fireEvent("addRoleSuccessEvent", output);
				}
			}
		});
		K.service(RoleDI.urls.URL_addRole, input);
	},

	/**
	 * 更新角色
	 */
	onUpdateRole : function(formElement) {
		var input = K.input({
			form : formElement,
			success : function(text) {
				var output = K.createOutput(text);
				if (!output.showError()) {
					E.fireEvent("updateRoleSuccessEvent", output);
				}
			}
		});
		K.service(RoleDI.urls.URL_updataRole, input);
	},

	/**
	 * 删除角色
	 */
	onDelRoles : function(ids) {
		var input = K.input({
			params : {
				ids : ids
			},
			success : function() {
				E.fireEvent("delRolesSuccessEvent");
			}
		});
		K.service(RoleDI.urls.URL_deleteRoleByIds, input);
	},

	/**
	 * 根据角色id查询关联的用户信息
	 */
	onGetUsersByRoleId : function(params) {
		var input = K.input({
			params : params,
			success : function(text) {
				var output = K.createOutput(text);
				if (!output.showError()) {
					E.fireEvent("getUsersByRoleIdSuccessEvent", output.get("users"));
				}
			}
		});
		K.service(RoleDI.urls.URL_gerUsersByRoleId, input);
	}
});